<!-- 代理管理 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="代理人名称">
                            <a-input v-model="agentName" placeholder="请输入代理人名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="客户名称">
                            <a-input v-model="customerName" placeholder="请输入客户名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="统一社会信用代码">
                            <a-input v-model="socialCode" placeholder="请输入统一社会信用代码" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='6' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="state" placeholder="请选择">
                                <a-select-option v-for="item in statusList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='10' :sm='24'>
                        <a-form-item label="时间">
                            <a-range-picker @change="onDateChange" />
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <a-button style="margin-left: 20px" type='' @click="add">添加代理业务</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ total: total ,showSizeChanger: true,pageSize: size,}" size="small">
            <div slot="state" slot-scope="state">
                <a-badge :status="state | contractStatusTypeFilter" :text="state | contractStatusFilter" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-popconfirm
                        title="是否启用当前代理?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="open(record)"
                        v-if="record.state == 0"
                    >
                        <a-button type="link">启用</a-button>
                    </a-popconfirm>
                    <a-popconfirm
                        title="是否停用当前代理?"
                        ok-text="是"
                        cancel-text="否"
                        @confirm="close(record)"
                        v-else
                    >
                        <a-button type="link">停用</a-button>
                    </a-popconfirm>
                </div>
            </div>
        </a-table>

        <a-modal 
            v-model="visible" 
            title="添加代理业务"
            @ok="handleOk" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="agency" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="客户名称" prop="customerId">
                        <a-select v-model="agency.customerId" placeholder="请选择" @change="selCustomer">
                            <a-select-option v-for="item in customerList" :key="item.id" :value="item.id">{{item.customerName}}</a-select-option>
                        </a-select>
                    </a-form-model-item>
                    <a-form-model-item label="统一社会信用代码">
                        <a-input v-model="agency.socialCode" :disabled="true" placeholder="请输入统一社会信用代码" />
                    </a-form-model-item>
                    <a-form-model-item label="直推代理人" prop="oneLevelUserName">
                        <a-input v-model="agency.oneLevelUserName" allow-clear placeholder="请输入直推代理人" />
                    </a-form-model-item>
                    <a-form-model-item label="直推身份证" prop="oneLevelIdCard">
                        <a-input v-model="agency.oneLevelIdCard" allow-clear placeholder="请输入直推身份证" />
                    </a-form-model-item>
                    <a-form-model-item label="间推代理人" >
                        <a-input v-model="agency.twoLevelUserName" :disabled="true" allow-clear placeholder="请输入间推代理人" />
                    </a-form-model-item>
                    <a-form-model-item label="间推身份证" >
                        <a-input v-model="agency.twoLevelIdCard" :disabled="true" allow-clear placeholder="请输入间推身份证" />
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { createAgent , getAgentList , updateStateAgent } from '@/api/services/agent';
import { queryCustomerList } from '@/api/services/customer';
export default {
    data () {
        return {
            customerName: '', // 客户名称
            agentName: '', // 代理名称
            socialCode: '', // 统一社会信用代码
            columns,
            page: 1,
            size: 10,
            isload: false,
            isAdd: false,
            dataList: [],
            total: 0,
            state: undefined,
            statusList: [
                { id: 0, name: '已停用'},
                { id: 1, name: '使用中'},
            ],
            visible: false,
            agency: {
                id: '',
                oneLevelUserName: '',
                oneLevelIdCard: '',
            },
            rules: {
                customerId: [{ required: true, message: '请选择客户信息', trigger: 'blur' }],
                oneLevelUserName: [{ required: true, message: '请输入直推代理人姓名', trigger: 'blur' }],
                oneLevelIdCard: [{ required: true, message: '请输入直推代理人身份证号码', trigger: 'blur' }],
            },
            customerList: [], // 客户列表
        };
    },
    async created(){
        await this.getCustomer();
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = `${dateStr[0]}`
                this.endTime = `${dateStr[1]}`
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this;
            _this.isload = true;
            const params = {
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'customerName': _this.customerName,
                'socialCode': _this.socialCode,
                'agentName': _this.agentName,
                'state': _this.state,
                'pageSize': _this.size,
                'pageIndex': _this.page,
            };
            getAgentList(params)
            .then(res => {
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 获取用户所有客户列表
        getCustomer(){
            let _this = this
            return new Promise((resolve,reject) => {
                const params = {
                    'pageSize': 99,
                    'pageIndex': 1,
                }
                queryCustomerList()
                .then(res=>{
                    console.log(res,'customerRes')
                    if(res.success){
                        _this.customerList = res.data;
                        resolve();
                    }else{
                        reject(res.errMessage);
                    }
                })
            })
        },
        // 添加代理业务
        add(){
            this.isAdd = true;
            this.visible = true
        },
        handleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    createAgent(_this.agency).then(res=>{
                        if(res.success){
                            _this.visible = false;
                            _this.getData();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: _this.isAdd ? '添加成功' : '修改成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 选择客户
        selCustomer(id){
            console.log(id,'val')
            var customer = this.customerList.find(item => item.id == id);
            this.agency.socialCode = customer.socialCode;
        },
        // 开启代理
        open(item){
            this.changeState(item.id,1);
        },
        // 关闭代理
        close(item){
            this.changeState(item.id,0);
        },
        // 代理状态切换
        changeState(id,state){
            let _this = this;
            const params = {
                'agentId': id,
                'state': state,
            };
            updateStateAgent(params)
            .then(res => {
                if(res.success){
                    _this.getData();
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '操作成功',
                        duration: 8
                    })
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>